

using Backoffice.Domain.Entities.App;
using Backoffice.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using UniversalAdmin.Shared.Extensions;
using UniversalAdmin.Shared.Pagination;

namespace Backoffice.Infrastructure.Repositories;
public class UserRepository : IUserRepository
{
    private readonly BackOfficeDbContext _context;
    public UserRepository(BackOfficeDbContext context) => _context = context;

    public async Task<PagedResult<User>> GetUsersPagedAsync(
        string? keyword,
        int pageIndex,
        int pageSize,
        CancellationToken ct)
    {
        var query = _context.AppUsers.AsNoTracking();

        if (!string.IsNullOrWhiteSpace(keyword))
            query = query.Where(u =>
                u.Username.Contains(keyword) ||
                u.Email.Contains(keyword));

        var dtoQuery = query.Select(u => new User
        {
            
            Username = u.Username,
            Email = u.Email,
            Phone = u.Phone
        });

        return await dtoQuery.ToPagedResultAsync(pageIndex, pageSize, ct);
    }
}